﻿//添加读者信息
void AddReader()
{
	FILE* pfReader;
	int ReaderRecord;
	int	FlagExist;//读者编号存在标记
	char Flag;//记录y/n选项

	system("cls");
	ReaderRecord = ReadReaderFile("ab+");//ab+追加方式打开或新建二进制文件
	if (ReaderRecord == -1)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	else if (ReaderRecord == 0)
	{
		cout << "\t没有读者信息" << endl;
	}
	else
	{
		ShowReader();//如果图书记录不为0则显示所有读者信息
	}

	//循环录入图书信息
	cout << "\t是否输入信息(y/n)";
	cin >> Flag;
	cout << endl;

	if (Flag == 'n')
	{
		return;
	}
	pfReader = fopen("reader.txt", "ab+");
	if (pfReader == NULL)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	while (Flag == 'y')
	{
		//录入数据
		if (ReaderRecord >= READER_NUM)
		{
			cout << "\t录入已满" << endl;
			fclose(pfReader);
			return;
		}
		//图书编号不能重复
		cout << "\t请输入读者编号：";

		do {
			FlagExist = 0;
			cin >> astReader[ReaderRecord].Num;
			cout << endl;

			for (int i = 0; i < ReaderRecord; i++)//循环判断图书编号，直到编号不重复为止 
			{
				if (astReader[i].Num == astReader[ReaderRecord].Num)
				{
					FlagExist = 1;
					cout << "\t该读者编号已经存在，请重新输入编号：";
					break;
				}
			}
		} while (FlagExist == 1);

		cout << "\t请输入读者姓名：";
		cin >> astReader[ReaderRecord].Name;
		cout << endl;

		cout << "\t请输入读者性别(F/M)：";
		cin >> astReader[ReaderRecord].Sex;
		cout << endl;
		while (astReader[ReaderRecord].Sex != 0)
		{

			if (strcmp(astReader[ReaderRecord].Sex, "F") == 0 || strcmp(astReader[ReaderRecord].Sex, "M") == 0)
			{
				break;
			}
			cout << "\t输入错误，只许输入'F'或'M',请重新输入";
			cin >> astReader[ReaderRecord].Sex;
			cout << endl;
		}

		cout << "\t请输入读者最大可借阅数量：";//todo可细化判断数量
		cin >> astReader[ReaderRecord].Max;
		cout << endl;

		cout << "\t请输入读者当前可借阅数量：";//两种一开始是相等的
		cin >> astReader[ReaderRecord].Amount;
		cout << endl;


		//将新增的读者信息写入到文件中
		if (fwrite(&astReader[ReaderRecord], LEN_READER, 1, pfReader) != 1)
		{
			cout << "\t无法保存该信息" << endl;
			return;
		}
		else
		{
			cout << "\t" << astReader[ReaderRecord].Num << "号读者信息已经保存" << endl;
			ReaderRecord++;
		}

		cout << endl;
		cout << "\t是否继续输入信息(y/n)：";
		cin >> Flag;
		cout << endl;
	}
	fclose(pfReader);
	if (Flag != 'y' && Flag != 'n')
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
	else
	{
		cout << "\t成功添加读者信息" << endl;
	}
}